//
//  PaperToolViewManager.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/2/24.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperToolViewManager.h"
#import "PaperToolView.h"
#import "PaperModuleModel.h"
#import "PaperTestModel.h"

@interface PaperToolViewManager ()

@property (nonatomic, strong) PaperToolView *view;
@property (nonatomic, strong, readwrite) RACSignal *backSignal;      // 返回Signal
@property (nonatomic, strong, readwrite) RACSignal *restSignal;      // 休息一下Signal
@property (nonatomic, strong, readwrite) RACSignal *penSignal;       // 画板Signal
@property (nonatomic, strong, readwrite) RACSignal *cardSignal;      // 答题卡Signal
@property (nonatomic, strong, readwrite) RACSignal *moreSignal;      // 更多功能Signal
@property (nonatomic, strong, readwrite) RACSignal *moveToSignal;    // 跳转功能Signal

@end

@implementation PaperToolViewManager
@dynamic view;

- (void)setupConfigure {
    self.view = [[PaperToolView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kNavigationBarHeight+40)];
    
    if (self.superView) {
        [self.superView addSubview:self.view];
    }

    self.backSignal = [self.view getSignalWithDictionary:@{@"action" : @"back"}];
    self.restSignal = [self.view getSignalWithDictionary:@{@"action" : @"rest"}];
    self.moveToSignal = [self.view getSignalWithDictionary:@{@"action" : @"module"}];
    
    self.restSignal = self.view.restSignal;
    [self.view.restSignal subscribeNext:^(id x) {
        //[self.view pauseTimer];
    }];
    [self.restSignal subscribeNext:^(id x) {        // 暂停计时
         [self.view pauseTimer];
    }];
    //self.view.surplus
    PaperModuleModel * model = self.datas.firstObject;
    self.view.surplus = model.paper_time*60;
}
- (void) closeTimer{
    [self.view endTimer];
    self.view = nil;
}
- (void)setTitle:(NSString *)title {
    _title = title;
    self.view.title = title;
}

- (void)setTimeSignal:(RACSignal *)timeSignal {
    _timeSignal = timeSignal;
    [_timeSignal subscribeNext:^(id x) {            // 重新计时
        [self.view restartTimer];
    }];
}

- (void)setSendSignal:(RACSignal *)sendSignal {
    _sendSignal= [sendSignal map:^id(NSMutableDictionary *dic) {
        dic[@"report_time"] = [self.view.startDate stringWithFormat:@"YYYY-MM-dd HH:mm:ss"];
        dic[@"report_sub_time"] = [[NSDate date] stringWithFormat:@"YYYY-MM-dd HH:mm:ss"];
        dic[@"report_time_long"] = [NSString stringWithFormat:@"%ld", self.view.duration];
        return dic;
    }];
}

- (void)setIndexSignal:(RACSignal *)indexSignal {
    _indexSignal = indexSignal;
    [_indexSignal subscribeNext:^(id value) {
        if ([value isKindOfClass:[PaperAnswerModel class]]) {
            [self.view showNormalType];     // 答题页面
            
            PaperAnswerModel *model = value;
            [self.view setNum:model.ques_num withCount:model.ques_count];
        } else if ([value isKindOfClass:[PaperModuleModel class]]) {
            NSUInteger index = [self.datas indexOfObject:value];
            if (index != NSNotFound) {
                [self.view setCurrentModuleAtIndex:index];
            }
            [self.view showModuleType];      // 模块介绍页
            
        } else if ([value isKindOfClass:[PaperTestModel class]]) {
            [self.view showNormalType];     // 申论自测页面
            
            PaperAnswerModel *model = value;
            [self.view setNum:model.ques_num withCount:model.ques_count];
        } else {
            [self.view showTimerType];      // 最后一页，答题卡页
        }
    }];
}

- (void)setShowToolTimer:(BOOL)showToolTimer {
    _showToolTimer = showToolTimer;
    self.view.showToolTimer = self.showToolTimer;
    if (showToolTimer == YES || showToolTimer == NO) {//无论是否显示计时控件，均计时
        [self.view startTimer];     // 开始计时
    }
}

- (void)setShowToolMore:(BOOL)showToolMore {
    _showToolMore = showToolMore;
    self.view.showToolMore = showToolMore;
    self.moreSignal = [self.view getSignalWithDictionary:@{@"action" : @"more"}];
}

- (void)setShowToolCard:(BOOL)showToolCard {
    _showToolCard = showToolCard;
    self.view.showToolCard = showToolCard;
    self.cardSignal = [self.view getSignalWithDictionary:@{@"action" : @"card"}];
}

- (void)setShowToolPen:(BOOL)showToolPen {
    _showToolPen = showToolPen;
    self.view.showToolPen = showToolPen;
    self.penSignal  = [self.view getSignalWithDictionary:@{@"action" : @"pen"}];
}

- (void)setShowToolSliderView:(BOOL)showToolSliderView {
    _showToolSliderView = showToolSliderView;
    self.view.showToolSliderView = showToolSliderView;
    self.view.height = showToolSliderView == YES ? 104 : kNavigationBarHeight;
}

- (void)setShowToolModuleTitleView:(BOOL)showToolModuleTitleView {
    _showToolModuleTitleView = showToolModuleTitleView;
    self.view.showToolModuleTitleView = showToolModuleTitleView;
    if (showToolModuleTitleView == YES) {
        self.view.height = 104;
        self.view.modules = self.datas;
    }
}
- (NSInteger)getAnswerDuration{
    return self.view.duration;
}

@end
